package org.liaohailong.transformapp;

import android.util.Log;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Author: liaohailong
 * Time: 2021/11/16 15:14
 * Describe: 方法耗时监控
 */
public class TimeMonitor {
    private static final String TAG = "TimeMonitor";

    private static final Map<String, Long> startRecord = new ConcurrentHashMap<>();

    // 采用这个作为 key ---> "{class_name}-{method_name}-{method-desc}"
    public static void start(String key) {
        startRecord.put(Thread.currentThread().getId() + key, System.currentTimeMillis());
    }

    public static void end(String key) {
        Long startTime = startRecord.remove(Thread.currentThread().getId() + key);
        if (startTime == null) return;
        long useTime = System.currentTimeMillis() - startTime;
        // 只打印执行超过 5ms 的方法
        if (useTime < 5) return;
        Log.i(TAG, "耗时：" + useTime + "，方法：" + key);
    }
}
